﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace QuanLySuKien
{
    public partial class KhuVuc : System.Web.UI.Page
    {
        string strCon = "Data Source=192.168.1.100;Initial Catalog=quanlysukien;User ID=sa;Password=123456";
        DataTable table = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            } 
        }

        void LoadData()
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("Select * from KhuVuc", strCon);
                adapter.Fill(table);
                GridView1.DataSource = table;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ex.Message;
            } 
        }

        //int Xoa(int makv)
        //{
        //    int i = 0;

        //    string query = "Delete from KhuVuc where MaKV = " + makv;
        //    try
        //    {
        //        using (SqlConnection connection = new SqlConnection(strCon))
        //        {
        //            using (SqlCommand command = new SqlCommand(query, connection))
        //            {
        //                connection.Open();
        //                i = command.ExecuteNonQuery();
        //            }
        //        }
        //        return i;
        //    }
        //    catch (Exception ex)
        //    {
        //        return 0;
        //    }
        //}

        //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int makv = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
        //    //int makv = 6;
        //    if (Xoa(makv) > 0)
        //    {
        //        LoadData();
        //    } 
        //}

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int row = GridView1.SelectedIndex;
            txtMaKV.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[0].Text);
            txtTenKV.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[1].Text);
            txtMoTa.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[2].Text);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool success = false;
            int i = 0;
            string Makv = txtMaKV.Text.ToString();
            string TenKhuVuc = txtTenKV.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();
            string query = "INSERT INTO KhuVuc (MaKV, TenKhuVuc, Mota) " + "VALUES (@prmMaKV, @prmTenKhuVuc, @prmMota)";
            try
            {
                using (SqlConnection connection = new SqlConnection(strCon))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        command.Parameters.Add("@prmMaKV", SqlDbType.VarChar, 10).Value = Makv;
                        command.Parameters.Add("@prmTenKhuVuc", SqlDbType.NVarChar, 100).Value = TenKhuVuc;
                        command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 200).Value = MoTa;

                        i = command.ExecuteNonQuery();
                        success = (i == 1);
                    }
                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã được cập nhật";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                    
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool success = false;
            int i = 0;
            string MaKhuVuc = txtMaKV.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();
            string TenKhuVuc = txtTenKV.Text.ToString();
            string query = "UPDATE KhuVuc SET TenKhuVuc=@prmTenKhuVuc, Mota=@prmMota WHERE MaKV=@prmMaKhuVuc";
            try
            {
                using (SqlConnection connection = new SqlConnection(strCon))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        command.Parameters.Add("@prmMaKhuVuc", SqlDbType.VarChar,10).Value = MaKhuVuc;
                        command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 200).Value = MoTa;
                        command.Parameters.Add("@prmTenKhuVuc", SqlDbType.NVarChar, 100).Value = TenKhuVuc;
                        i = command.ExecuteNonQuery();
                        success = (i == 1);
                    }
                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã được cập nhật";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                   
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            bool success = false;
            int i = 0;
            string MaKhuVuc = txtMaKV.Text.ToString();
            string query = "DELETE FROM KhuVuc WHERE MaKV=@prmMaKhuVuc";
            try
            {
                using (SqlConnection connection = new SqlConnection(strCon))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        command.Parameters.Add("@prmMaKhuVuc", SqlDbType.VarChar,10).Value = MaKhuVuc;
                        i = command.ExecuteNonQuery();
                        success = (i == 1);
                    }
                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã được xóa";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                    
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể xóa";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            txtMaKV.Text = "";
            txtTenKV.Text = "";
            txtMoTa.Text = "";
            StatusLabel.Text = "";
        }

        //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    GridView1.PageIndex = e.NewPageIndex;
        //    GridView1.DataSource = table;
        //    GridView1.DataBind();
        //}

        //protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    GridView1.EditIndex = Convert.ToInt16(e.NewEditIndex);
        //    HiddenField1.Value = e.NewEditIndex.ToString();
        //    LoadData();
        //}

        //protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    TextBox t1 = new TextBox();
        //    TextBox t2 = new TextBox();
        //    TextBox t3 = new TextBox();
        //    //TextBox t4 = new TextBox();

        //    t1 = (TextBox)GridView1.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[1].Controls[0];
        //    t2 = (TextBox)GridView1.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[2].Controls[0];
        //    t3 = (TextBox)GridView1.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[3].Controls[0];
        //    //t4 = (TextBox)GridView1.Rows[Convert.ToInt32(HiddenField1.Value)].Cells[4].Controls[0];

        //    //table.Rows[e.RowIndex]["Mã Khu Vực"] = t1.Text;
        //    table.Rows[e.RowIndex]["Tên Khu Vực"] = t1.Text;
        //    table.Rows[e.RowIndex]["Thành Phố"] = t2.Text;
        //    table.Rows[e.RowIndex]["Mô Tả"] = t3.Text;

        //    GridView1.EditIndex = -1;
        //    LoadData();
        //}

        //protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        //{
        //    GridView1.EditIndex = -1;
        //    LoadData();
        //} 

        
    }
}